package com.bytedance.improvingroad.scroll_conflict

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.viewpager.widget.ViewPager
import kotlin.math.abs

/**
 * Created by chenxiong
 * date 12/21/21
 */
class MyPager(context: Context, attributeSet: AttributeSet? = null): ViewPager(context, attributeSet) {

    private var lastX = 0f
    private var lastY = 0f
    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        /*
        1. 内部拦截法
         if (ev?.action == MotionEvent.ACTION_DOWN) {
        super.onInterceptTouchEvent(ev)
        return false
        }
        return true
         */
        when(ev?.action) {
            MotionEvent.ACTION_DOWN -> {
                lastX = ev.x
                lastY = ev.y
            }
            MotionEvent.ACTION_MOVE -> {
                if (abs(ev.x-lastX) > abs(ev.y-lastY)) {
                    return true
                }
            }
        }
        return super.onInterceptTouchEvent(ev)
    }
}